package com.xuan.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.xuan.base.constant.MessageConstant;
import com.xuan.entity.vo.SecurityUser;
import com.xuan.entity.UserDTO;
import com.xuan.service.feign.UserRemoteApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.security.authentication.AccountExpiredException;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description: TODO 加载用户信息
 * @date 2021/7/22 14:50
 */
@Service
@Slf4j
public class UserServiceImpl implements UserDetailsService {

    //private List<UserDTO> userList;

    //@Autowired
   // private PasswordEncoder passwordEncoder;


    /*@PostConstruct
    public void initData() {
        String password = passwordEncoder.encode("123456");

        log.info("用户密码为=============>"+password);

        //模拟用户信息
        userList = new ArrayList<>();

        userList.add(new UserDTO(1L,"macro", password,1, CollUtil.toList("ADMIN"),1));

        userList.add(new UserDTO(2L,"andy", password,1, CollUtil.toList("TEST"),1));
    }*/

    @Autowired
    private UserRemoteApiService userRemoteApiService;

    /**
     * @description: 校验用户信息
     * @param username
     * @return: org.springframework.security.core.userdetails.UserDetails
     * @author Lenovo
     * @date: 2021/7/23 9:35
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //使用list流中过滤方法便利出每个用户进行校验成功后返回集合
       // List<UserDTO> findUserList = userList.stream().filter(item -> item.getUsername().equals(username)).collect(Collectors.toList());

        log.info("检验用户信息");
        List<UserDTO> findUserList = userRemoteApiService.getUserByUserName(username);
        log.info("后台查询数据结果为==========>"+findUserList);

        if (CollUtil.isEmpty(findUserList)) {//判断数组中是否有值 为空则返回异常
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        //有的话存到securityUser
        SecurityUser securityUser = new SecurityUser(findUserList.get(0));

        log.info("用户状态为================>"+securityUser.isEnabled());

        if (!securityUser.isEnabled()) {
            throw new DisabledException(MessageConstant.ACCOUNT_DISABLED);
        } else if (!securityUser.isAccountNonLocked()) {
            throw new LockedException(MessageConstant.ACCOUNT_LOCKED);
        } else if (!securityUser.isAccountNonExpired()) {
            throw new AccountExpiredException(MessageConstant.ACCOUNT_EXPIRED);
        } else if (!securityUser.isCredentialsNonExpired()) {
            throw new CredentialsExpiredException(MessageConstant.CREDENTIALS_EXPIRED);
        }
        return securityUser;
    }
}
